<?php
/**
 * $Id: resources.php,v 1.00 21 June 2005 John N Exp $
 * Module: WF-Links
 * Version: v1.0.3
 * Release Date: 21 June 2005
 * Developer: John N
 * Team: WF-Projects
 * Licence: GNU
 */

include 'admin_header.php';

$op = wfl_cleanRequestVars( $_REQUEST, 'op', '' );
$rid = wfl_cleanRequestVars( $_REQUEST, 'rid', '' );
/**
 * edit_resource()
 * 
 * @param integer $rid 
 * @return 
 */
function edit_resource( $rid = 0 )
{
    global $xoopsDB;

    $res_arr = array();
    $res_arr['rid'] = isset( $_GET['rid'] ) ? intval( $_GET['rid'] ) : 0;
    $res_arr['lid'] = isset( $_GET['lid'] ) ? intval( $_GET['lid'] ) : 0;
    $res_arr['rtid'] = isset( $_POST['rtid'] ) ? intval( $_POST['rtid'] ) : 0;
    $res_arr['name'] = isset( $_POST['name'] ) ? $_POST['name'] : '';
    $res_arr['description'] = isset( $_POST['description'] ) ? $_POST['description'] : '';
    $res_arr['url'] = isset( $_POST['url'] ) ? $_POST['url'] : '';
    $res_arr['item_id'] = isset( $_POST['item_id'] ) ? intval( $_POST['item_id'] ) : 0;

    $q_restypes = $xoopsDB -> query( "SELECT rtid, name FROM " . $xoopsDB -> prefix( "wf_resource_types" ) . " ORDER by name asc" );

    if ( $rid != 0 && $_GET['op'] != 'changert' )
    {
        $query = "select * from " . $xoopsDB -> prefix( 'wf_resources' ) . " WHERE rid = $rid";
        $res_arr = $xoopsDB -> fetchArray( $xoopsDB -> query( $query ) );
    } 
    // ----------------------------
    // Create/edit form definition
    // ----------------------------
    echo "<script type='text/javascript'><!--";
    echo "
      function changeResourceType() {
         document.op.op.value = '" . wfl_cleanRequestVars( $_REQUEST, 'op', '' ) . "';
         document.op.action = 'resources.php?op=changert&lid=" . $res_arr['lid'] . "&rid=" . $res_arr['rid'] . "&title=" . $_REQUEST['title'] . "';
         document.op.submit();
      }
    ";
    echo "// --></script>";

    echo "<div style='text-align: center;'><h3>=====> " . $_REQUEST['title'] . " <=====</h3></div>";

    $forminfo = ( $rid == 0 ) ? _AM_WFL_RES_CREATEF : _AM_WFL_RES_MODIFYF;
    $sform = new XoopsThemeForm( $forminfo , "op", xoops_getenv( 'PHP_SELF' ) );

    $res_typeSelect = new XoopsFormSelect( _AM_WFL_RES_TYPE, "rtid", $res_arr['rtid'] );
    $res_typeSelect -> setExtra( "onChange='changeResourceType();'" );
    $res_typeSelect -> addOption( "0", "---" );
    while ( $res_type = $xoopsDB -> fetchArray( $q_restypes ) )
    {
        $res_typeSelect -> addOption( $res_type['rtid'], $res_type['name'] );
    } 
    $sform -> addElement( $res_typeSelect, true ); 
    // Look for current resource type info
    $q_restype = $xoopsDB -> query( "SELECT resource_list_select, module_id, url FROM " . $xoopsDB -> prefix( "wf_resource_types" ) . " WHERE rtid=" . $res_arr['rtid'] . " ORDER by name asc" );
    $res_type = $xoopsDB -> fetchArray( $q_restype ); 
    // if no sql => show following fields
    if ( empty( $res_type['resource_list_select'] ) )
    {
        if ( intval( $res_arr['rtid'] ) > 0 )
        {
            $sform -> addElement( new XoopsFormText( _AM_WFL_RES_NAMEF, 'name', 50, 255, $res_arr['name'] ), true );
            $sform -> addElement( new XoopsFormTextArea( _AM_WFL_RES_DESCF, 'description', $res_arr['description'], 5, 60 ), false );
            $sform -> addElement( new XoopsFormText( _AM_WFL_RES_URLF, 'url', 50, 255, $res_arr['url'] ), false );
            $sform -> addElement( new XoopsFormHidden( 'item_id', $res_arr['item_id'] ) );
        } 
    } 
    else
    { 
        // execute  resource_list_select and construct select form item (item_id)
        $sform -> addElement( new XoopsFormTextArea( _AM_WFL_RES_DESCF, 'description', $res_arr['description'], 5, 60 ), false );
        $q_items = $xoopsDB -> query( $res_type['resource_list_select'] );

        $itemSelect = new XoopsFormSelect( _AM_WFL_RES_ITEMIDF, "item_id", $res_arr['item_id'] );
        $itemSelect -> addOption( "0", "------------------" );
        while ( list( $it_id, $it_name ) = $xoopsDB -> fetchRow( $q_items ) )
        {
            $itemSelect -> addOption( $it_id, $it_name );
        } 
        $itemSelect -> setExtra( "onChange='document.op.name.value=document.op.item_id.options[selectedIndex].text;'" );
        $sform -> addElement( $itemSelect, true );
        $sform -> addElement( new XoopsFormHidden( 'name', $res_arr['name'] ) );
        $sform -> addElement( new XoopsFormHidden( 'url', $res_arr['url'] ) );
    } 

    $sform -> addElement( new XoopsFormHidden( 'rid', $res_arr['rid'] ) );
    $sform -> addElement( new XoopsFormHidden( 'lid', $res_arr['lid'] ) );
    $sform -> addElement( new XoopsFormHidden( 'title', $_REQUEST['title'] ) );

    $button_tray = new XoopsFormElementTray( '', '' );
    $hidden = new XoopsFormHidden( 'op', 'save' );
    $button_tray -> addElement( $hidden );

    if ( intval( $res_arr['rtid'] ) > 0 )
    {
        if ( !$rid )
        {
            $butt_create = new XoopsFormButton( '', '', _AM_WFL_BCREATE, 'submit' );
            $butt_create -> setExtra( 'onclick="this.form.elements.op.value=\'save\'"' );
            $button_tray -> addElement( $butt_create );

            $butt_clear = new XoopsFormButton( '', '', _AM_WFL_BRESET, 'reset' );
            $button_tray -> addElement( $butt_clear );
        } 
        else
        {
            $butt_create = new XoopsFormButton( '', '', _AM_WFL_BMODIFY, 'submit' );
            $butt_create -> setExtra( 'onclick="this.form.elements.op.value=\'save\'"' );
            $button_tray -> addElement( $butt_create );
        } 
        $butt_cancel = new XoopsFormButton( '', '', _AM_WFL_BCANCEL, 'button' );
        $butt_cancel -> setExtra( 'onclick="history.go(-1)"' );
        $button_tray -> addElement( $butt_cancel );
    } 
    $sform -> addElement( $button_tray );
    $sform -> display();
} 

switch ( strtolower( $op ) )
{
    case 'save':

        $rid = ( isset( $_POST['rid'] ) && $_POST['rid'] > 0 ) ? $_POST['rid'] : 0;
        $lid = intval( $_POST['lid'] );
        $rtid = intval( $_POST['rtid'] );
        $name = $wfmyts -> addslashes( $_POST['name'] );
        $description = $wfmyts -> addslashes( $_POST['description'] );
        $url = $_POST['url'];
        $item_id = intval( $_POST['item_id'] );
        $mid = $xoopsModule -> getVar( 'mid' );

        if ( $rid == 0 )
        {
            $query = "INSERT INTO " . $xoopsDB -> prefix( "wf_resources" )
             . "(rid, lid, rtid, mid, name, description, url, item_id ) VALUES"
             . "('', '$lid', '$rtid','$mid','$name', '$description', '$url','$item_id')";
        } 
        else
        {
            $query = "UPDATE " . $xoopsDB -> prefix( "wf_resources" )
             . " SET rtid = '$rtid', name = '$name', description = '$description',"
             . "url = '$url', item_id = $item_id WHERE rid = '$rid'";
        } 
        $error = "Could not update resource information:";
        $error .= "<br /><br />" . $query;
        $result = $xoopsDB -> queryF( $query );
        if ( !$result )
        {
            trigger_error( $error, E_USER_ERROR );
        } 
        $dbupted = ( $rid == 0 ) ? _AM_WFL_RES_CREATED : _AM_WFL_RES_MODIFIED;
        redirect_header( "resources.php?lid=$lid&title=" . $_REQUEST['title'], 1, $dbupted );
        break;

    case "delete":

        global $xoopsDB;

        $confirm = ( isset( $_POST['confirm'] ) ) ? 1 : 0;

        if ( $confirm )
        {
            $sql = "DELETE FROM " . $xoopsDB -> prefix( "wf_resources" ) . " WHERE rid = " . $_POST['rid'] . "";
            $result = $xoopsDB -> query( $sql );

            if ( $result )
            {
                redirect_header( "resources.php?lid=" . $_GET['lid'] . "&title=" . $_REQUEST['title'], 1, sprintf( _AM_WFL_RES_DELETED, $_POST['name'] ) );
            } 
            else
            {
                $error = "" . _AM_WFLL_EVENNEWS_DBERROR . ": <br /><br />" . $sql;
                trigger_error( $error, E_USER_ERROR );
            } 
            exit();
        } 
        else
        {
            $rid = ( isset( $_POST['rid'] ) ) ? $_POST['rid'] : $rid;
            $result = $xoopsDB -> query( "SELECT rid, name FROM " . $xoopsDB -> prefix( "wf_resources" ) . " WHERE rid = '$rid'" );
            list( $rid, $name ) = $xoopsDB -> fetchrow( $result );
            xoops_cp_header();
            xoops_confirm( array( 'op' => 'delete', 'rid' => $rid, 'confirm' => 1, 'name' => $name ), 'resources.php?lid=' . $_GET['lid'] . '&title=' . $_REQUEST['title'], _AM_WFL_RES_DELETETHIS . "<br /><br>" . $name, _AM_WFL_RES_DELETE );
            xoops_cp_footer();
        } 
        break;

    case 'edit':
        xoops_cp_header();
        wfl_adminmenu( _AM_WFL_MRESOURCES );
        edit_resource( $_GET['rid'] );
        xoops_cp_footer();
        break;

    case 'main':
    default:

        Global $xoopsModuleConfig;
        $start = isset( $_GET['start'] ) ? intval( $_GET['start'] ) : 0;
        $lid = isset( $_GET['lid'] ) ? intval( $_GET['lid'] ) : 0;

        $query = "SELECT r.lid, r.rid, r.rtid, r.name, r.description, r.url res_url, r.item_id, rt.name restypename, rt.url res_type_url FROM " . $xoopsDB -> prefix( 'wf_resources' ) . " r, " . $xoopsDB -> prefix( 'wf_resource_types' ) . " rt WHERE rt.rtid = r.rtid AND lid = " . $lid . " ORDER BY rt.name, r.name";
        $res_array = $xoopsDB -> query( $query, 20, $start );

        $res_num = $xoopsDB -> getRowsNum( $xoopsDB -> query( $query ) );

        xoops_cp_header();
        wfl_adminmenu( _AM_WFL_MRESOURCES );

        echo "
		<fieldset><legend style='font-weight: bold; color: #0A3760;'>" . _AM_WFL_RES_MODIFYF . "</legend>\n
		<div style='padding: 8px;'>" . _AM_WFL_RES_INFOTEXT . "</div>\n
		</fieldset>\n
		";

        edit_resource();

        echo "
		</fieldset><br />\n
		<table border='0' width='100%' cellpadding='2' cellspacing='1' class='outer'>\n
		<tr>\n
		";

        $headingarray = array( _AM_WFL_RES_ID, _AM_WFL_RES_TYPE, _AM_WFL_RES_NAME, _AM_WFL_RES_DESC, _AM_WFL_ACTION );
        for( $i = 0; $i <= count( $headingarray )-1; $i++ )
        {
            $align = ( $i == 1 ) ? "left" : "center";
            echo "<td align='$align' class='bg3'><b>" . $headingarray[$i] . "</td>";
        } 
        echo "</tr>";
        if ( $res_num > 0 )
        {
            while ( $resources = $xoopsDB -> fetchArray( $res_array ) )
            {
                $maintitle = urlencode( $wfmyts -> htmlSpecialChars( trim( $_REQUEST['title'] ) ) );
                $image_array = array( "<a href='resources.php?op=edit&amp;rid=" . $resources['rid'] . "&title=" . $maintitle . "'>" . $imagearray['editimg'] . "</a> <a href='resources.php?op=delete&amp;lid=" . $resources['lid'] . "&rid=" . $resources['rid'] . "&title=" . $maintitle . "'>" . $imagearray['deleteimg'] . "</a>" );
                echo "<tr>";
                echo "<td style='text-align: center;' class='head'>" . $resources['rid'] . "</td>";
                echo "<td class='even'>" . $resources['restypename'] . "</td>";
                if ( !empty( $resources['item_id'] ) && !empty( $resources['res_type_url'] ) )
                {
                    echo "<td class='even'><a target='_blank' href='" . XOOPS_URL . $resources['res_type_url'] . $resources['item_id'] . "'>" . $resources['name'] . "</a></td>";
                } 
                else
                {
                    echo "<td class='even'><a target='_blank' href='" . $resources['res_url'] . "'>" . $resources['name'] . "aaaa</a></td>";
                } 
                echo "<td class='even'>" . $resources['description'] . "</td>";
                echo "<td style='text-align: center;' class='even'>";
                foreach ( $image_array as $images )
                {
                    echo $images;
                } 
                echo "</td></tr>";
            } 
        } 
        else
        {
            echo "<tr><td style='text-align: center;' class='head' colspan='5'>" . _AM_WFL_SUB_NOFILESWAITING . "</td></tr>";
        } 

        echo "</table>\n";

        include_once XOOPS_ROOT_PATH . '/class/pagenav.php';
        $page = ( $res_num > 20 ) ? _AM_WFL_MINDEX_PAGE : '';
        $pagenav = new XoopsPageNav( $res_num, 20, $start, 'start' );
        echo "<div style='text-align: right; padding: 8px;'>" . $page . '' . $pagenav -> renderNav() . '</div>';
        xoops_cp_footer();
} 

?>